package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.DishFlavorDto;
import com.sky.dto.DishPageDto;
import com.sky.entity.Dish;
import com.sky.exception.BusinessException;
import com.sky.exception.extend.CategoryOfDishesAreEmptyException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.IDishService;
import com.sky.vo.DishPageVo;
import com.sky.vo.DishVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品相关的表现层
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
    @Autowired
    private IDishService dishService;
    /**
     * 菜品分页查询的表现层业务逻辑
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult<DishPageVo>> page(DishPageDto dishPageDto){
        PageResult<DishPageVo> dishPageVoPageResult=dishService.queryByPage(dishPageDto);
        if (dishPageVoPageResult==null){
            throw new BusinessException(MessageConstant.UN_KNOW_ERROR);
        }else {
            return Result.success(dishPageVoPageResult);
        }
    }

    /**
     * 新增菜品
     * @param dishFlavorDto
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody DishFlavorDto dishFlavorDto){
        /*log.info("{}",dishFlavorDto.toString());测试成功
        log.info("{}",dishFlavorDto.getImage());*/
        dishService.saveDish(dishFlavorDto);
        //
        return Result.success("菜品新增成功");
    }

    /**
     * 根据分类id来查询关联的所有菜品
     * @param categoryId
     * @return
     */
    @GetMapping("list")
    public Result<List<Dish>> list(@RequestParam(value = "categoryId") Long categoryId){
        //log.info("{}",categoryId); 测试成功
        List<Dish> dishes=dishService.listByCategoryId(categoryId);
        if(dishes.isEmpty()){
            throw new CategoryOfDishesAreEmptyException(MessageConstant.DISHES_AER_EMPTY);
        }else {
            return Result.success(dishes);
        }
    }

    /**
     *
     * @param status :菜品状态
     * @param id :菜品主键
     * @return :String 响应情况 是否成功
     */
    @PostMapping("/status/{status}")
    public Result<String> modifyDishStatus(@PathVariable(value = "status")Integer status,Long id){
        //log.info("{},{}",status,id); 测试成功
        Dish dish = Dish.builder().id(id)
                .status(status).build();

        dishService.updateStatus(dish);
        return Result.success("修改菜品状态成功");
    }
    @GetMapping("/{id}")
    public Result<DishVo> queryById(@PathVariable(value = "id")Long id){
        return Result.success(dishService.getById(id));
    }

    /**
     * 修改菜品
     * @param dishFlavorDto :传入的数据
     * @return : 返回一个字符串
     */
    @PutMapping
    public Result<String> modifyDishWithFlavor(@RequestBody DishFlavorDto dishFlavorDto){
        //log.info("{},{}",dishFlavorDto.getFlavors().toArray(),dishFlavorDto.getId());测试成功
        dishService.modifyDish(dishFlavorDto);
        return Result.success("菜品修改成功");
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> deleteDishWithFlavor(@RequestParam(value = "ids") List<Long> ids){
        dishService.deleteDish(ids);
        return Result.success("删除成功");
    }
}
